package com.qingke.blog;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.servlet.http.HttpServlet;


import com.qingke.pojo.Article;
import com.qingke.pojo.User;

public class Dao /*extends HttpServlet*/{

	public Dao() {
		
	}
	public List<Article> getAllArticles() throws SQLException{
		Connection conn =getConnection();
		PreparedStatement stat=null;
		ResultSet rs=null;
		Article article=null;
		User user=null;
		List<Article> articles =new ArrayList<>();
		String sql="select * from article";
		stat =conn.prepareStatement(sql);
		rs=stat.executeQuery();
		while(rs.next()){
			Integer id=rs.getInt("id");
			String title=rs.getString("title");
			String content=rs.getString("content");
			String createdAt=rs.getString("created_at");
			Integer userId=rs.getInt("user_id");
			user=getUserById(userId);
			article=new Article(userId, title, content, createdAt, user);
			articles.add(article);
		}
		
		return articles;
	}
	public User getUserById(Integer id) throws SQLException{
		Connection conn =getConnection();
		PreparedStatement stat=null;
		ResultSet rs=null;
		User user=null;
		String sql="select * from user where id=?";
		stat=conn.prepareStatement(sql);
		stat.setInt(1, id);
		rs=stat.executeQuery();
		while(rs.next()){
			Integer uid=rs.getInt("id");
			String username=rs.getString("username");
			String password=rs.getString("password");
			String createdAt=rs.getString("created_at");
			user=new User(uid, username, password, createdAt);
		}
		return user;
	}

	public  Connection getConnection(){
		Connection conn=null;
//		String driver="com.mysql.jdbc.Driver";
//		String url = "jdbc:mysql://localhost:3306/student_manage";
//		String u = "root";
//		String p = "root";
		String driver=getProperties("db.driver");
		String url = getProperties("db.url");
		String u = getProperties("db.username");
		String p = getProperties("db.password");
		
		try {
			Class.forName(driver);
			conn =DriverManager.getConnection(url, u, p);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	private String getProperties(String key) {
		Properties props = new Properties();
		try {
			String path="C:\\Users\\qingke04\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\Blog\\jdbc.properties";
			//InputStream in= (InputStream) getServletContext().getResourceAsStream("/jdbc.properties");
			//FileInputStream in= new FileInputStream(path);
			//String path=getServletContext().getRealPath("/jdbc.properties");
			//System.out.println(path);
			FileInputStream in= new FileInputStream(path);
			props.load(in);
			return props.getProperty(key);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	public void cleanup(Connection conn, PreparedStatement statement, ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		if (statement != null) {
			try {
				statement.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
	}
}
